<?php
$OJ_CACHE_SHARE=false;
$cache_time=60;
require_once("config/path_config.php");
require_once('./include/db_info.inc.php');
require_once('./include/cache_start.php');
require_once('./include/setlang.php');
require_once('./config/sys_config.php');

$view_title= "题库-题目列表";
// 判断用户是否已登录
if (!isset($_SESSION['user_id'])) {
    $error_url="loginpage.php";
    $error_desc=$MSG_Login;
    require("template/bs3/error.php");
    exit(0);
}

// 1. 获取查询参数 -- 题目类别和题目名称
$searchParam = '';
$searchStr = '';
if( isset($_GET['search']) && (trim($_GET['search'])!= "") ) {
    $searchParam = $_GET['search'];
    $searchStr = "%".$searchParam."%";
}

// 2. 获取查询参数 -- 页码
$pageNo = -1;
if ( isset($_GET['page']) ) {
    $pageNo = intval($_GET['page']);
}

// 3. 更新用户最后访问的页码
// 要求：(1). 有传入页码   (2). 未进行搜索操作  (3). 用户已登录
if ( $pageNo > 0 && $searchStr == '' && isset($_SESSION['user_id']) ) {
    $sql = "update users set volume=? where user_id=?";
    pdo_query($sql, $pageNo, $_SESSION['user_id']);
}

// 若用户未传入页面则获取用户上次离开时所处的页码
// 1. 未传入页码  2. 未进行搜索操作  3. 用户已登录
if ( $pageNo < 0 && $searchStr == '' && isset($_SESSION['user_id']) ) {
    $sql = "select volume from users where user_id=?";
    $result = pdo_query($sql, $_SESSION['user_id']);
    $row = null;
    if ( $result ) {
        $row = $result[0];
    }
    if ( isset($row) ) {
        $pageNo = intval($row[0]);
    }
}

// 补全参数 $pageNo
if( !is_numeric($pageNo)|| $pageNo < 0 ) {
    $pageNo = 1;
}

// 查询过滤条件
$filterSql = "";
$args = array();
if ( isset($_SESSION['administrator']) ){
    $filterSql = "";
}
else if ( isset($_SESSION['problem_editor']) ) {
    $filterSql = " where (`defunct`='N' or `creater_id`=?) ";
    array_push($args, $_SESSION['user_id']);
}
else {
    $filterSql = " where `defunct`='N' ";
}

// 查询过滤条件 -- 题目类型和题目标题
if( $searchStr != "" ) {
    if ( strlen($filterSql) > 0 ) {
        $filterSql .= " and (`title` like ? or `source` like ?) ";
    }
    else {
        $filterSql .= " where (`title` like ? or `source` like ?) ";
    }
    array_push($args, $searchStr, $searchStr);
}

// 查询题目总数
$sql = "select count(`problem_id`) from problem";
$sql .= $filterSql;

$result = DbUtil::getInstance()->query2($sql, $args);
$row = null;
if ( $result ) {
    $row = $result[0];
}
$totalCnt = 0;
if ( isset($row) ) {
    $totalCnt = $row[0];
}

// 每页展示的题目数量
$questionCntPerPage = SysConfig::$PAGESIZE;

// 页面总数
$pageCnt = ceil($totalCnt / $questionCntPerPage);

// 当前页面题目的起始索引号
$questionIdxStart =  ($pageNo - 1) * $questionCntPerPage;

// 查询索所有题目,本页面值能查看所有激活的题目
$sql="SELECT `problem_id`,`title`,`source`,`submit`,`accepted` , `level`  FROM `problem`";
$sql.= $filterSql;
$sql.= " ORDER BY `level`, `problem_id`  limit ".$questionIdxStart.", ".$questionCntPerPage;
$result = DbUtil::getInstance()->query2($sql, $args);

$view_problemset=Array();
$i=0;
foreach ($result as $row)
{
    $view_problemset[$i]=Array();

	// 用户未登录
	if ( !isset($_SESSION['user_id']) ) {
        $view_problemset[$i][0]= "<div class=none> </div>";
    }
    else {
        // 查看代码是否已AC
        $acSql = "SELECT `result` FROM `solution` WHERE `user_id`=? and `problem_id`=? GROUP BY `result`";
        $acResult = pdo_query($acSql, $_SESSION['user_id'], $row['problem_id']);
        // 未提交过本题的代码
        if ( ! $acResult ) {
            $view_problemset[$i][0]= "<div class=none> </div>";
        }
        else {
            $acRow = $acResult[0];
            // 已AC
            $para = "problem_id=".$row['problem_id']."&user_id=".$_SESSION['user_id'];
            if ( intval($acRow[0]) == 4 ) {
                $view_problemset[$i][0]="<a href='status.php?".$para."'><div class='btn btn-success'>Y</div></a>";
            }
            else {
                $view_problemset[$i][0]="<a href='status.php?".$para."'><div class='btn btn-danger'>N</div></a>";
            }
        }
    }

	$view_problemset[$i][1]="<div class='center'>".SysConfig::$PID_PREFIX.$row['problem_id']."</div>";;
	$view_problemset[$i][2]="<div class='left'><a href='problem.php?id=".$row['problem_id']."'>".$row['title']."</a></div>";;
	$view_problemset[$i][3]="<div class='center'><nobr>".$row['source']."</nobr></div >";
    $view_problemset[$i][4]="<div class='center'>".$row['level']."</div>";

    $allowDelSolution = isset($_SESSION['administrator']);
    if ( $allowDelSolution ) {
        $view_problemset[$i][5]="<div class='center'><a href='admin/status_list.php?problem_id=".$row['problem_id']."&jresult=4'>".$row['accepted']."</a></div>";
    }
    else {
        $view_problemset[$i][5]="<div class='center'><a href='status.php?problem_id=".$row['problem_id']."&jresult=4'>".$row['accepted']."</a></div>";
    }
	$view_problemset[$i][6]="<div class='center'><a href='status.php?problem_id=".$row['problem_id']."'>".$row['submit']."</a></div>";

	$i++;
}

require("template/".$OJ_TEMPLATE."/problemset.php");
if(file_exists('./include/cache_end.php'))
	require_once('./include/cache_end.php');

if ( file_exists("template/bs3/oj-footer.php") )
    require("template/bs3/oj-footer.php");
?>
